package com.woniuxy.util;

import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;

import java.util.List;
import java.util.stream.Collectors;


public class ObjectConverter {
    private static final ModelMapper modelMapper = new ModelMapper();

    static {
        // 配置严格匹配策略
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    }

    // 单个对象转换
    public static <S, T> T convert(S source, Class<T> targetClass) {
        return modelMapper.map(source, targetClass);
    }

    // 列表对象转换
    public static <S, T> List<T> convertList(List<S> sourceList, Class<T> targetClass) {
        return sourceList.stream()
                .map(source -> modelMapper.map(source, targetClass))
                .collect(Collectors.toList());
    }
}
